package cn.iocoder.idata.module.idata.controller.admin.template;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.idata.framework.common.pojo.PageResult;
import cn.iocoder.idata.framework.common.pojo.CommonResult;
import static cn.iocoder.idata.framework.common.pojo.CommonResult.success;

import cn.iocoder.idata.framework.excel.core.util.ExcelUtils;

import cn.iocoder.idata.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.idata.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.idata.module.idata.controller.admin.template.vo.*;
import cn.iocoder.idata.module.idata.dal.dataobject.template.TemplateDO;
import cn.iocoder.idata.module.idata.convert.template.TemplateConvert;
import cn.iocoder.idata.module.idata.service.template.TemplateService;

@Tag(name = "管理后台 - 标注模板管理")
@RestController
@RequestMapping("/idata/template")
@Validated
public class TemplateController {

    @Resource
    private TemplateService templateService;

    @PostMapping("/create")
    @Operation(summary = "创建标注模板管理")
    @PreAuthorize("@ss.hasPermission('idata:template:create')")
    public CommonResult<Long> createTemplate(@Valid @RequestBody TemplateCreateReqVO createReqVO) {
        return success(templateService.createTemplate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新标注模板管理")
    @PreAuthorize("@ss.hasPermission('idata:template:update')")
    public CommonResult<Boolean> updateTemplate(@Valid @RequestBody TemplateUpdateReqVO updateReqVO) {
        templateService.updateTemplate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除标注模板管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('idata:template:delete')")
    public CommonResult<Boolean> deleteTemplate(@RequestParam("id") Long id) {
        templateService.deleteTemplate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得标注模板管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('idata:template:query')")
    public CommonResult<TemplateRespVO> getTemplate(@RequestParam("id") Long id) {
        TemplateDO template = templateService.getTemplate(id);
        return success(TemplateConvert.INSTANCE.convert(template));
    }

    @GetMapping("/list")
    @Operation(summary = "获得标注模板管理列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('idata:template:query')")
    public CommonResult<List<TemplateRespVO>> getTemplateList(@RequestParam("ids") Collection<Long> ids) {
        List<TemplateDO> list = templateService.getTemplateList(ids);
        return success(TemplateConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得标注模板管理分页")
    @PreAuthorize("@ss.hasPermission('idata:template:query')")
    public CommonResult<PageResult<TemplateRespVO>> getTemplatePage(@Valid TemplatePageReqVO pageVO) {
        PageResult<TemplateDO> pageResult = templateService.getTemplatePage(pageVO);
        return success(TemplateConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出标注模板管理 Excel")
    @PreAuthorize("@ss.hasPermission('idata:template:export')")
    @OperateLog(type = EXPORT)
    public void exportTemplateExcel(@Valid TemplateExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<TemplateDO> list = templateService.getTemplateList(exportReqVO);
        // 导出 Excel
        List<TemplateExcelVO> datas = TemplateConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "标注模板管理.xls", "数据", TemplateExcelVO.class, datas);
    }

}
